import 'package:boss/models/offers.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/offersManagement/constant/offers_constant.dart';
import 'package:boss/ui/page/offersManagement/page/offersDetail/provider/offers_detail_provider.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class OffersDetailDataCard extends StatelessWidget {
  String string2Weekday(String value) {
    if (value == '') return '--';
    List<String> val = ['一', '二', '三', '四', '五', '六', '日'];
    var valarr = value.split('|').map((item) => val[int.parse(item)]);
    return '星期' + valarr.join('、');
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        padding: EdgeInsets.symmetric(
          vertical: 10.w,
        ),
        child: ShadowRadiusCard(
          width: double.infinity,
          margin: EdgeInsets.symmetric(
            horizontal: 20.w,
          ),
          padding: EdgeInsets.only(
            left: 30.w,
            right: 30.w,
            bottom: 30.w,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _Title(
                title: '活动规则',
              ),
              Consumer<OffersDetailProvider>(
                builder: (context, offerDetailProvider, _) {
                  return Column(
                    children: <Widget>[
                      _TitleValue(
                        title: '优惠名称',
                        value: offerDetailProvider.data.activityName,
                      ),
                      _TitleValue(
                        title: '优惠类型',
                        value: offerDetailProvider.data.discountTypeString,
                      ),
                      _TitleValue(
                        title: '活动日期',
                        value:
                            '${offerDetailProvider.data.startDate == '' ? '--' : '${StringUtil.line2Backslash(offerDetailProvider.data.startDate)}-${StringUtil.line2Backslash(offerDetailProvider.data.endDate)}'}',
                      ),
                      _TitleValue(
                        title: '时间',
                        value: offerDetailProvider.data.startTime == '' ? '--' : '${offerDetailProvider.data.startTime}-${offerDetailProvider.data.endTime}',
                      ),
                      _TitleValue(
                        title: '星期',
                        value: string2Weekday(offerDetailProvider.data.weekTime),
                      ),
                      _buildOrderDiscount(offerDetailProvider),
                      _buildPlanDiscount(offerDetailProvider),
                      _buildFullDiscount(offerDetailProvider),
                      _buildSpecialOffers(offerDetailProvider),
                      _buildDoubleOffers(offerDetailProvider),
                      _buildBuyGiftOffers(offerDetailProvider),
                    ],
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  //整单折扣
  Widget _buildOrderDiscount(OffersDetailProvider offersDetailProvider) {
    if (offersDetailProvider.data.discountType == OffersConstant.order_discount) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _TitleValue(
            title: '整单折扣率',
            value: '${offersDetailProvider.data.orderDiscountList[0].discountRate}%',
          ),
          _Title(
            title: '不参与活动菜品',
            count: offersDetailProvider.data.orderDiscountCount,
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = offersDetailProvider.data.orderDiscountList[index];

              return _TitleValue(
                title: current.comPropertiesDTO.commodityAndSkuName,
                value: '${AmountUtil.price2String(current.comPropertiesDTO.price)}',
              );
            },
            itemCount: offersDetailProvider.data.orderDiscountCount,
          ),
        ],
      );
    }

    return SizedBox();
  }

  //方案折扣
  Widget _buildPlanDiscount(OffersDetailProvider offersDetailProvider) {
    if (offersDetailProvider.data.discountType == OffersConstant.plan_discount) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _Title(
            title: '分类折扣率',
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = offersDetailProvider.data.planMenuDiscountList[index];

              return _TitleValue(
                title: '${current.menuName}(共${current.count}个菜品)',
                value: '${current.discountRate}%',
              );
            },
            itemCount: offersDetailProvider.data.planMenuDiscountList.length,
          ),
        ],
      );
    }
    return SizedBox();
  }

  //满减
  Widget _buildFullDiscount(OffersDetailProvider offersDetailProvider) {
    if (offersDetailProvider.data.discountType == OffersConstant.full_discount) {
      return Column(
        children: <Widget>[
          _Title(
            title: '满减规则',
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = offersDetailProvider.data.fullDiscountList[index];

              return _TitleValue(
                title: '优惠${index + 1}',
                value:
                    '满${AmountUtil.price2String(current.fullCount)}，减${AmountUtil.price2String(current.reductionCount)}元',
              );
            },
            itemCount: offersDetailProvider.data.fullDiscountList.length,
          ),
        ],
      );
    }
    return SizedBox();
  }

  //特价菜
  Widget _buildSpecialOffers(OffersDetailProvider offersDetailProvider) {
    if (offersDetailProvider.data.discountType == OffersConstant.special_discount) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _Title(
            title: '活动菜品',
            count: offersDetailProvider.data.foodDiscountList.length,
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = offersDetailProvider.data.foodDiscountList[index];

              return _TitleValue(
                title: current.comPropertiesDTO.commodityAndSkuName,
                value: '优惠价：${AmountUtil.price2String(current.comPropertiesDTO.price * current.discountRate ~/ 100)}',
              );
            },
            itemCount: offersDetailProvider.data.foodDiscountCount,
          ),
        ],
      );
    }
    return SizedBox();
  }

  //第二份
  Widget _buildDoubleOffers(OffersDetailProvider offersDetailProvider) {
    if (offersDetailProvider.data.discountType == OffersConstant.double_discount) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _TitleValue(
            title: '第二份优惠方式',
            value: offersDetailProvider.data.secondDiscountList[0].secondType == 0
                ? '折扣（第二份折扣率${offersDetailProvider.data.secondDiscountList[0].discountRate}%）'
                : '减免（第二份减免${AmountUtil.price2String(offersDetailProvider.data.secondDiscountList[0].reductionCount)}元）',
          ),
          _Title(
            title: '活动菜品',
            count: offersDetailProvider.data.secondDiscountList.length,
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = offersDetailProvider.data.secondDiscountList[index];
              String price;
              if (current.secondType == 0) {
                price = AmountUtil.price2String(current.comPropertiesDTO.price * current.discountRate ~/ 100);
              } else {
                price = AmountUtil.price2String(current.comPropertiesDTO.price - current.reductionCount);
              }

              return _TitleValue(
                title: current.comPropertiesDTO.commodityAndSkuName,
                value: '优惠价：$price',
              );
            },
            itemCount: offersDetailProvider.data.secondDiscountList.length,
          ),
        ],
      );
    }

    return SizedBox();
  }

  //买赠
  Widget _buildBuyGiftOffers(OffersDetailProvider offersDetailProvider) {
    if (offersDetailProvider.data.discountType == OffersConstant.buy_gift_discount) {
      List<ComPropertiesDTO> buyCommoditys = [];
      offersDetailProvider.data.buyGiveDiscountlist.forEach((item) {
        if (!buyCommoditys.any((temp) => temp.commodityId == item.buyComPropertiesDTO.commodityId && temp.skuId == item.buyComPropertiesDTO.skuId)) {
          buyCommoditys.add(item.buyComPropertiesDTO);
        }
      });

      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _Title(
            title: '购买${offersDetailProvider.data.buyGiveDiscountlist[0].buyCount}份赠送${offersDetailProvider.data.buyGiveDiscountlist[0].giveCount}份',
          ),
          _Title(
            title: '购买菜品',
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = buyCommoditys[index];
              return _TitleValue(
                title: current.commodityAndSkuName,
                value: '¥${AmountUtil.price2String(current.price)}',
              );
            },
            itemCount: buyCommoditys.length,
          ),
          _Title(
            title: '赠送菜品',
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var current = offersDetailProvider.data.buyGiveDiscountlist[index];
              return _TitleValue(
                title: current.giveComPropertiesDTO.commodityAndSkuName,
                value: '¥${AmountUtil.price2String(current.giveComPropertiesDTO.price)}',
              );
            },
            itemCount: offersDetailProvider.data.buyGiveDiscountlist.length,
          ),
        ],
      );
    }

    return SizedBox();
  }
}

class _TitleValue extends StatelessWidget {
  final String title;
  final String value;

  const _TitleValue({Key key, this.title, this.value}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 65.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Expanded(
            child: Text(
              title ?? '--',
              style: TextStyleConstant().normal_3_28,
              maxLines: 1,
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: TextStyleConstant().normal_1_28,
              maxLines: 1,
              textAlign: TextAlign.end,
            ),
          ),
        ],
      ),
    );
  }
}

class _Title extends StatelessWidget {
  final String title;
  final int count;

  const _Title({
    Key key,
    this.title,
    this.count,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 93.w,
      alignment: Alignment.centerLeft,
      child: RichText(
        text: TextSpan(text: title, style: TextStyleConstant().normal_1_32, children: [
          if (count != null) TextSpan(text: '（$count）', style: TextStyleConstant().normal_1_24),
        ]),
      ),
    );
  }
}
